require 'middleclass'
local vector2d = require("Vector2D")

MathUtils = class('MathUtils')
--
MathUtils.static.RIGHT = 'RIGHT'
MathUtils.static.LEFT = 'LEFT'

function MathUtils.static.dist(object1,object2)
    return  math.sqrt((object1.x - object2.x)^2 + (object1.y - object2.y)^2 )
end

function MathUtils.static.angleBetween ( srcObj, dstObj )
    local vect = Vector2D:Sub(srcObj, dstObj)
    local angleBetween = math.deg( math.atan( vect.y/vect.x ) )
    if ( srcObj.x < dstObj.x ) then 
        angleBetween = angleBetween + 90 
    else 
        angleBetween = angleBetween + 270 
    end
    return angleBetween
end

function MathUtils.static.direction ( srcObj, dstObj, linearVelocity )
    if ( srcObj.x < dstObj.x ) then 
        if(linearVelocity.y < 0)then
            return MathUtils.RIGHT
        else
            return MathUtils.LEFT
        end
    else 
        if(linearVelocity.y < 0)then
            return MathUtils.LEFT
        else
            return MathUtils.RIGHT
        end
    end
end

function MathUtils.static.rotate ( origin ,vector, ang )
    local result = {}
    
    local vect = Vector2D:Sub(vector, origin)
    result.x = origin.x + (vect.x * math.cos(ang) - vect.y * math.sin(ang))
    result.y = origin.y + (vect.x * math.sin(ang) + vect.y * math.cos(ang))
    return result
end

function MathUtils.static.rotateV (vector, ang )
    return MathUtils.rotate ( {x=0, y=0} ,vector, ang )
end
   
function MathUtils.static.isOver (player, block )
    local vector = vector2d:Sub(block:getVector(),player:getVector())
    local rVector = MathUtils.rotate ( {x=0, y=0} ,vector, math.rad(block.body.rotation))
    print(rVector.x..':'..rVector.y)
    if(rVector.y > block.body.height/2 + player.body.height/2 -5)then
        return true
    end
    return false
end
